-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: add OnStart hook for synchronous startup jobs #2079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add OnStart hook for synchronous startup jobs #2079
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should i update iin readme.md , it would be okay to do so right ? @Umang01-hash |
@HeerakKashyap I think we can update the existing docs or maybe create a new one for the same. Both can work depending upon the content we are sharing. |
- Move OnStart logic to a dedicated, testable method. - Fix all linter issues (deep-exit and cyclomatic complexity). - Add unit tests for success and error cases. - Add comprehensive documentation with a realistic example. - Update the example app to demonstrate a real-world use case.
57c4743
to
8526959
Compare
![]() |
…own on context cancel)
…hyap/gofr into fix/onstart-di-context
kidnly have a look @ccoVeille @Umang01-hash |
The code is in, but I would prefer if the context was not forged twice. Can't we create the gofr.Context before calling startup hooks, then pass it through all the layers as argument up to the place where the gofr.Context is created now? Or would this implies breaking changes,? Or simply too much changes? Please note, it doesn't necessary have to be addressed in this PR. |
Thank you for the suggestion Sir! @ccoVeille I agree that creating the gofr.Context only once and passing it through would be cleaner and more efficient. For this PR, I’ll keep the current approach to avoid introducing breaking changes, but I think this is a great idea for future improvement and can be considered in a dedicated refactor. |
@Umang01-hash sir, kindly run the workflows and let me know is there now any linter errors etc. |
Hey @HeerakKashyap only a few things:
Please do the above changes and we should be ready to go with your PR. |
- Fix dynamic error definition - Fix unused parameters with underscore - Use t.Context() instead of context.Background() - Use assert.NoError instead of assert.Nil
- Fix cuddled return statement in newContextForHooks - Fix cuddled assignment in OnStart test - Remove os.Exit call to resolve deep-exit error
f4882d6
to
9e8bcdf
Compare
@coolwednesday The distinction between context.Canceled (graceful shutdown):
Example scenarios:
I believe this allows users to distinguish between intentional shutdowns and actual startup problems. |
@HeerakKashyap , Please address all the review comments, so that we can close this ASAP. |
I've given replies above for the same , so should i do all the changes given by the maintainers ? |
If we are thinking about graceful exit. It should happen in all cases. That is all the DB/Stream Connections started should be closed with graceful exit. Keep Context.Canceled only. |
Yes Please address them. |
…hyap/gofr into fix/onstart-di-context
…hyap/gofr into fix/onstart-di-context
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some documentation fix. Rest LGTM.
Thanks !
Co-authored-by: ccoVeille <[email protected]>
1db6c03
* Fix/pubsub migration get last version (#2119) * add chaining of next migrator in getLastVersion method of pubsub.go * fix linter * feat: add OnStart hook for synchronous startup jobs (#2079) * feat: improve OnStart hook with DI context, update docs, and ensure code quality * refactor: improve OnStart hook context, move logic to method, and test DI access * feat(onstart): implement all reviewer feedback - Move OnStart logic to a dedicated, testable method. - Fix all linter issues (deep-exit and cyclomatic complexity). - Add unit tests for success and error cases. - Add comprehensive documentation with a realistic example. - Update the example app to demonstrate a real-world use case. * fix: improve startup error handling for OnStart hooks (graceful shutdown on context cancel) * fix: address linter errors in OnStart tests - Fix dynamic error definition - Fix unused parameters with underscore - Use t.Context() instead of context.Background() - Use assert.NoError instead of assert.Nil * fix: remove os.Exit call to resolve deep-exit linter error * fix: resolve remaining linter errors - Fix cuddled return statement in newContextForHooks - Fix cuddled assignment in OnStart test - Remove os.Exit call to resolve deep-exit error * refactor: remove newContextForHooks and reuse existing newContext function * fix: resolve linting errors and refactor Run() method for better maintainability - Fix err113: Replace dynamic error creation with static error in TestApp_OnStart - Fix gocyclo: Refactor Run() method by extracting helper methods to reduce cyclomatic complexity - Fix ws1: Correct whitespace issues in gofr_test.go and run.go - Add helper methods: handleStartupHooks, startShutdownHandler, startTelemetryIfEnabled, startAllServers - Improve code organization and readability while maintaining functionality * fix: resolve remaining linting issues in TestApp_OnStart - Fix err113: Move error variable to package level to avoid dynamic error creation - Fix error-naming: Rename hookFailedErr to errHookFailed following Go naming conventions - Fix whitespace: Remove unnecessary blank line before app assignment - All linting issues in modified files are now resolved * fix: resolve godot and wsl linter errors in run.go and gofr_test.go * Update go.work.sum * fix: restore comments as requested by reviewer * Update run.go * Update run.go * fix: address reviewer comments - add godoc comment to OnStart method and move startup-hooks.md to proper directory structure * fix: restore metrics server comments and ensure OnStart hook runs before route registration * fix: implement proper context usage in runOnStartHooks and fix example API usage * Update go.work.sum * fix: correct spelling of 'canceled' in comment * minor documentation fix * revert go.sum changes * Update pkg/gofr/gofr_test.go Co-authored-by: ccoVeille <[email protected]> * revert go.work.sum changes --------- Co-authored-by: HeerakKashyap <[email protected]> Co-authored-by: Umang Mundhra <[email protected]> Co-authored-by: coolwednesday <[email protected]> Co-authored-by: Divya Darshana <[email protected]> Co-authored-by: ccoVeille <[email protected]> * build(deps): bump github.com/prometheus/client_golang Bumps [github.com/prometheus/client_golang](https://github.com/prometheus/client_golang) from 1.22.0 to 1.23.0. - [Release notes](https://github.com/prometheus/client_golang/releases) - [Changelog](https://github.com/prometheus/client_golang/blob/main/CHANGELOG.md) - [Commits](prometheus/client_golang@v1.22.0...v1.23.0) --- updated-dependencies: - dependency-name: github.com/prometheus/client_golang dependency-version: 1.23.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * build(deps): bump go.opentelemetry.io/otel/exporters/prometheus Bumps [go.opentelemetry.io/otel/exporters/prometheus](https://github.com/open-telemetry/opentelemetry-go) from 0.59.0 to 0.59.1. - [Release notes](https://github.com/open-telemetry/opentelemetry-go/releases) - [Changelog](https://github.com/open-telemetry/opentelemetry-go/blob/main/CHANGELOG.md) - [Commits](open-telemetry/opentelemetry-go@exporters/prometheus/v0.59.0...exporters/prometheus/v0.59.1) --- updated-dependencies: - dependency-name: go.opentelemetry.io/otel/exporters/prometheus dependency-version: 0.59.1 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> * build(deps): bump gofr.dev in /examples/using-add-filestore Bumps [gofr.dev](https://github.com/gofr-dev/gofr) from 1.42.4 to 1.42.5. - [Release notes](https://github.com/gofr-dev/gofr/releases) - [Commits](v1.42.4...v1.42.5) --- updated-dependencies: - dependency-name: gofr.dev dependency-version: 1.42.5 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> * build(deps): bump github.com/ClickHouse/clickhouse-go/v2 Bumps [github.com/ClickHouse/clickhouse-go/v2](https://github.com/ClickHouse/clickhouse-go) from 2.39.0 to 2.40.1. - [Release notes](https://github.com/ClickHouse/clickhouse-go/releases) - [Changelog](https://github.com/ClickHouse/clickhouse-go/blob/main/CHANGELOG.md) - [Commits](ClickHouse/clickhouse-go@v2.39.0...v2.40.1) --- updated-dependencies: - dependency-name: github.com/ClickHouse/clickhouse-go/v2 dependency-version: 2.40.1 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * build(deps): bump crate-ci/typos in the actions group Bumps the actions group with 1 update: [crate-ci/typos](https://github.com/crate-ci/typos). Updates `crate-ci/typos` from 1.34.0 to 1.35.1 - [Release notes](https://github.com/crate-ci/typos/releases) - [Changelog](https://github.com/crate-ci/typos/blob/master/CHANGELOG.md) - [Commits](crate-ci/typos@v1.34.0...v1.35.1) --- updated-dependencies: - dependency-name: crate-ci/typos dependency-version: 1.35.1 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: actions ... Signed-off-by: dependabot[bot] <[email protected]> * build(deps): bump github.com/prometheus/client_golang Bumps [github.com/prometheus/client_golang](https://github.com/prometheus/client_golang) from 1.22.0 to 1.23.0. - [Release notes](https://github.com/prometheus/client_golang/releases) - [Changelog](https://github.com/prometheus/client_golang/blob/main/CHANGELOG.md) - [Commits](prometheus/client_golang@v1.22.0...v1.23.0) --- updated-dependencies: - dependency-name: github.com/prometheus/client_golang dependency-version: 1.23.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * build(deps): bump google.golang.org/api from 0.243.0 to 0.244.0 Bumps [google.golang.org/api](https://github.com/googleapis/google-api-go-client) from 0.243.0 to 0.244.0. - [Release notes](https://github.com/googleapis/google-api-go-client/releases) - [Changelog](https://github.com/googleapis/google-api-go-client/blob/main/CHANGES.md) - [Commits](googleapis/google-api-go-client@v0.243.0...v0.244.0) --- updated-dependencies: - dependency-name: google.golang.org/api dependency-version: 0.244.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * build(deps): bump modernc.org/sqlite from 1.38.1 to 1.38.2 Bumps [modernc.org/sqlite](https://gitlab.com/cznic/sqlite) from 1.38.1 to 1.38.2. - [Commits](https://gitlab.com/cznic/sqlite/compare/v1.38.1...v1.38.2) --- updated-dependencies: - dependency-name: modernc.org/sqlite dependency-version: 1.38.2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> * build(deps): bump github.com/golang-jwt/jwt/v5 from 5.2.3 to 5.3.0 Bumps [github.com/golang-jwt/jwt/v5](https://github.com/golang-jwt/jwt) from 5.2.3 to 5.3.0. - [Release notes](https://github.com/golang-jwt/jwt/releases) - [Changelog](https://github.com/golang-jwt/jwt/blob/main/VERSION_HISTORY.md) - [Commits](golang-jwt/jwt@v5.2.3...v5.3.0) --- updated-dependencies: - dependency-name: github.com/golang-jwt/jwt/v5 dependency-version: 5.3.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * Merge pull request #2135 from gofr-dev/fix-docs * update release version to v1.43.0 --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: HootingConjuror <[email protected]> Co-authored-by: HeerakKashyap <[email protected]> Co-authored-by: coolwednesday <[email protected]> Co-authored-by: Divya Darshana <[email protected]> Co-authored-by: ccoVeille <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Description:
OnStart
hook for the GoFr framework, allowing users to register synchronous startup jobs that execute before the server starts.*gofr.Context
with all DI-managed services, as requested by maintainers.Testing:
Fixes #1833
Let me know if you want to add or change anything, or if you need help attaching screenshots or filling out the checklist!